package com.keshan.controller;

import com.auth0.jwt.interfaces.DecodedJWT;
import com.keshan.pojo.*;
import com.keshan.service.BloodPressureServices;
import com.keshan.service.UserDeviceServices;
import com.keshan.utils.DateUtil;
import com.keshan.utils.JWTUtils;
import com.keshan.utils.Msg;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.io.UnsupportedEncodingException;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author keshan
 * @date 2022/10/21 - 2022/10/23
 */
@CrossOrigin
@Controller
@RequestMapping("/bloodpressure")
public class BloodPressureController {
    @Autowired
    BloodPressureServices bloodPressureServices;
    @Autowired
    UserDeviceServices userDeviceServices;

    @RequestMapping("/insertbloodpressure")
    @ResponseBody
    public Msg insertBySelectiveToBloodPressure(@RequestBody BloodPressureJson bloodPressureJson) {
        Date date = new Date();
        System.out.println(bloodPressureJson.toString());
        List<UserDevice> userDeviceList = userDeviceServices.getUserDeviceListByImei(bloodPressureJson.getImei());
        try {
            BloodPressure bloodPressure = new BloodPressure();
            bloodPressure.setDbp(bloodPressureJson.getDbp());
            bloodPressure.setDbpL(bloodPressureJson.getDbp_l());
            bloodPressure.setDeviceType(bloodPressureJson.getDevice_type());
            bloodPressure.setUsername(userDeviceList.get(0).getUsername());
            bloodPressure.setSbp(bloodPressureJson.getSbp());
            bloodPressure.setSbpH(bloodPressureJson.getSbp_h());
            bloodPressure.setTimeBegin(DateUtil.StringToDate(bloodPressureJson.getTime_begin_str()));
            System.out.println(bloodPressure.toString());
            int result = bloodPressureServices.insertBySelectiveBloodPressure(bloodPressure);
            return Msg.success().add("result", result);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return Msg.failInsert();

    }


    @RequestMapping("/bloodpressurelisthidetoday")
    @ResponseBody
    public Msg selectBloodPressureListHideByImeiToday(HttpServletRequest httpServletRequest) {
        Map<String, String> map = new HashMap<>();
        String token = httpServletRequest.getHeader("token");
        System.out.println(token);
        try {
            DecodedJWT decodedJWT = JWTUtils.verifyAndInfo(token);
            String username = decodedJWT.getClaim("username").asString();
            System.out.println(username);
            List<BloodPressureHide> bloodPressureList = bloodPressureServices
                    .getBloodPressureListHideByUsernameToday(username);
            return Msg.success().add("bloodPressureList", bloodPressureList);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return Msg.fail().add("error", "查询失败");
    }

    /**
     * 查询当年前后两个月每日数据平均值
     *
     * @param httpServletRequest
     * @return
     */
    @RequestMapping("/bloodpressurelistdayavg")
    @ResponseBody
    public Msg selectBloodPressureListByImeiDayAvg(HttpServletRequest httpServletRequest) {
        Map<String, String> map = new HashMap<>();
        String token = httpServletRequest.getHeader("token");
        System.out.println(token);
        try {
            DecodedJWT decodedJWT = JWTUtils.verifyAndInfo(token);
            String username = decodedJWT.getClaim("username").asString();
            System.out.println(username);
            List<BloodPressureDayAvg> bloodPressureDayAvgList = bloodPressureServices
                    .getbloodpressureListEverydayAvgMonth(username);
            return Msg.success().add("bloodPressureDayAvgList", bloodPressureDayAvgList);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return Msg.fail().add("error", "查询失败");
    }
}
